package com.niodata.dp.monitor.components.task.service;

import com.niodata.dp.monitor.components.task.dao.TaskExecDayCountMapper;
import com.niodata.dp.monitor.components.task.dao.TaskExecStatisticMapper;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskExecCount;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskExecDayCount;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskFailedObj;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskStaticObj;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by adventl on 2018/5/7.
 */
@Service("taskStatisticService")
public class TaskStatisticService {

  @Autowired
  TaskExecDayCountMapper execDayCountMapper;
  @Autowired
  private TaskExecStatisticMapper executionMapper;

  private static List<String> getBetweenDates(String start, String end, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    List<String> result = new ArrayList<String>();
    result.add(start);
    Calendar tempStart = Calendar.getInstance();
    try {
      tempStart.setTime(sdf.parse(start));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    tempStart.add(Calendar.DAY_OF_YEAR, 1);

    Calendar tempEnd = Calendar.getInstance();
    try {
      tempEnd.setTime(sdf.parse(end));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    while (tempStart.before(tempEnd)) {
      result.add(sdf.format(tempStart.getTime()));
      tempStart.add(Calendar.DAY_OF_YEAR, 1);
    }
    result.add(end);
    return result;
  }

  // get task num
  public Map<String, Integer> getTaskCount(long ownerId, List<Long> ids,
        String startDate, String endDate) {

    return executionMapper.getTaskExecutionCount(ownerId, ids, startDate, endDate);
  }

  public List<TaskStaticObj> getHourTaskCount(long ownerId, List<Long> ids,
        String startDate, String endDate) {

    return executionMapper.getHourTaskCount(ownerId, ids, startDate, endDate);
  }

  public List<TaskStaticObj> getWeekTaskCount(long ownerId, List<Long> ids,
        String startDate, String endDate) {

    return executionMapper.getWeekTaskCount(ownerId, ids, startDate, endDate);
  }

  public List<TaskFailedObj> getFailedTaskCount(long ownerId, List<Long> ids,
        String startDate, String endDate, String format) {

    return executionMapper.getFailedCount(ownerId, ids, startDate, endDate, format);
  }

  /**
   * 获取执行次数 .
   *
   * @param ownerId ownerId
   * @param ids ids
   * @param startDate startDate
   * @param endDate endDate
   * @return result
   */
  public List<TaskExecCount> getExecDayCount(long ownerId, List<Long> ids,
        String startDate, String endDate) {
    //查询endDate之前的所有数据
    List<TaskExecDayCount> execDayCountList =
          execDayCountMapper.getExecDayCount(ownerId, ids, endDate);

    if (null == execDayCountList && execDayCountList.size() < 1) {
      return null;
    }

    List<String> dateList = getBetweenDates(startDate, endDate, "yyyyMMdd");
    List<TaskExecCount> taskExecCountList = new ArrayList<>();
    //循环累加出执行总数
    for (String date : dateList) {
      Integer sum = 0;
      for (TaskExecDayCount execDayCount : execDayCountList) {
        if (Integer.valueOf(execDayCount.getDataTime()) <= Integer.valueOf(date)) {
          sum += execDayCount.getExecCount();
        }
      }

      TaskExecCount taskExecCount = new TaskExecCount();
      taskExecCount.setExecCount(sum);
      taskExecCount.setTime(date);
      taskExecCountList.add(taskExecCount);
    }

    return taskExecCountList;
  }

}
